const mongoose = require('mongoose')
const Schema = mongoose.Schema

const messageCollection = new Schema({
    nickname : { type: String, required: true },
    content : { type: String, required: true },
    createDate : { type: String, required: true },
    avatar : { type: String, required: true },
    ip : { type: String, required: true },
    blogId: {
      type: Schema.Types.ObjectId,
      ref: 'Blog',
      default: null,
    },
})
messageCollection.methods.toJSON = function () {
  const obj = mongoose.Document.prototype.toJSON.call(this);
  const blogIdType = typeof obj.blogId;
  if (blogIdType === 'object') {
    if (obj.blogId) {
      obj.blog = obj.blogId;
    }
    delete obj.blogId;
  }
  obj.id = obj._id;
  return obj;
};
const Message = mongoose.model('Message', messageCollection)

module.exports = Message
